package lab.gene.dao;

import java.util.List;

import lab.gene.pojo.GeneQuickChange;
import lab.gene.pojo.Gene;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

@SuppressWarnings("deprecation")
public class GeneQuickChangeDAO {
	private static SessionFactory factory = new AnnotationConfiguration().configure().addAnnotatedClass(GeneQuickChange.class).addPackage("com.xyz").buildSessionFactory();
	
	@SuppressWarnings("unchecked")
	public GeneQuickChange getGeneQuickChangeByNameAndType(Gene gene, String type) {
		GeneQuickChange qch= null;
		try {
			Session session = factory.openSession();
	        try {
	        	Query query = session.createQuery("select g from GeneQuickChange as g where g.id = (select max(gg.id) from GeneQuickChange as gg where gg.type = '"+type+"' and gg.gene=:gene)").setParameter("gene", gene);
	        	List<GeneQuickChange> results = query.list();
	        	System.out.println("gene quickchange size:"+results.size());
	        	if (results.size() > 0) {
	        		qch = results.get(0);
	        		System.out.println("gene quickchange found!"); 
	        	}
	        	else {
	        		System.out.println("result.size = "+ results.size());
	        		System.out.println("no gene quickchange found");
	        	}
	        }
	        catch (HibernateException e) {
	        	e.printStackTrace();
	        } 
	        finally {
	            session.close(); 
	        }
	   } 
		catch (Throwable ex) { 
			System.err.println("Failed to create sessionFactory object." + ex);
	        throw new ExceptionInInitializerError(ex);
	   }
		return qch;
	}
	
	public void addGeneQuickChange(GeneQuickChange qch) {
		try {
	         Session session = factory.openSession();
	         Transaction tx = null;
	         try {
	        	 tx = session.beginTransaction();
	        	 session.save(qch); 
	        	 tx.commit();
	        	 } 
	         catch (HibernateException e) {
	        	 if (tx!=null) 
	        		 tx.rollback();
	        	 e.printStackTrace(); 
	         } 
	         finally {
	            session.close(); 
	         }
	      } catch (Throwable ex) { 
	         System.err.println("Failed to create sessionFactory object." + ex);
	         throw new ExceptionInInitializerError(ex); 
	      }
	}
	
	@SuppressWarnings("unchecked")
	public List<GeneQuickChange> getGeneQuickChangeHistory(Gene gene, String type) {
		List<GeneQuickChange> list = null;
		try {
			Session session = factory.openSession();
	        try {
	        	Query query = session.createQuery("select g from GeneQuickChange as g where g.type = '"+type+"' and g.gene=:gene order by id asc").setParameter("gene",gene); 
	        	List<GeneQuickChange> results = query.list();
	        	if (results.size() > 0) {
	        		list = results;
		        }
	        }
	        catch (HibernateException e) {
	        	e.printStackTrace();
	        } 
	        finally {
	            session.close(); 
	        }
	   } 
		catch (Throwable ex) { 
			System.err.println("Failed to create sessionFactory object." + ex);
	        throw new ExceptionInInitializerError(ex);
	   }
		return list;
	}
	

}
